﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Windows.Forms;

namespace 实现简单注册窗体
{
    public partial class RegForm : Form
    {
        public RegForm()
        {
            InitializeComponent();
            ShowCode();//调用生成验证码的方法
        }
        ValidateCode code = new ValidateCode();//实例化生成验证码的类
        private string vcode = string.Empty;//声明存储验证码的字符串变量
       
        //生成验证码的方法
        private void ShowCode()
        {
            vcode = code.CreateValidateCode(4);//把生成的验证码赋值给变量
            byte[] buffer = code.CreateValidateGraphic(vcode);//把字符串转成字节流
            label4.Image = ByteToImage(buffer);//把字节流转成图片显示在窗体中的pictureBox1控件
        }
        /// <summary>
        /// 字节流转成图片
        /// </summary>
        /// <param name="buffer">需要转换的字节流</param>
        /// <returns>返回生成图片</returns>
        private Image ByteToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            return Image.FromStream(ms, true);
        }


        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            String repwd = textBox3.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("密码不能为空！");
                return;
            }
            //else if (!textBox2.Text.Equals(textBox3.Text))
            //{
              //  MessageBox.Show("两次输入的密码不一致！");
            //}
            else if (textBox3.Text.Trim() != vcode)
            {
                this.Text = "验证码错误";
                textBox3.Focus();//输入错误，输入框获取焦点
            }
            else
            {
                this.Text = "验证码正确";
            }

            MainForm mainForm = new MainForm();
            mainForm.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ShowCode();
        }
    }
}
